package service;



import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import static util.DBUtil.conn;

// 订单服务类
public class OrderService {
//    private static List<Order> orders = new ArrayList<>();

    public static String createOrder(String username, String screeningId, List<Integer[]> seats) {
        String screening = ScreeningService.getScreeningById(screeningId);
        double  price;
        String stime;
        if (screening == null) {
            return null;
        }

        try(PreparedStatement ptmt=conn.prepareStatement("SELECT * FROM screening WHERE id=?");){
            ptmt.setString(1,screeningId);
            ResultSet rs= ptmt.executeQuery();
            if (rs.next()) {
                price=rs.getDouble("price");
                stime=rs.getString("start_time");
            }
            else{
                return null;
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        double totalPrice = seats.size() * price;
        String orderId = "ORD" + System.currentTimeMillis();
        Date orderTime = new Date();

        try(PreparedStatement ptmt=conn.prepareStatement("INSERT INTO orders (order_id, user_name,title, seat, order_time, price,stime,state) VALUES (?, ?, ?, ?, ?, ?,?,?)");){
            ptmt.setString(1, orderId);
            ptmt.setString(2, username);
            ptmt.setString(3, screening);
            ptmt.setString(4, seats.stream()
                    .flatMap(Arrays::stream)       // 扁平化：合并所有数组元素
                    .map(String::valueOf)          // 每个元素转为字符串
                    .collect(Collectors.joining(",")));
            ptmt.setTimestamp(5, new java.sql.Timestamp(orderTime.getTime()));
            ptmt.setDouble(6, totalPrice);
            ptmt.setString(7, stime);
            ptmt.setString(8, "待支付");
            ptmt.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


        LogService.logAction(username, "创建订单: " + orderId);
        return orderId;
    }

    public static void payOrder(String username,String orderId) {
        try(PreparedStatement ptmt=conn.prepareStatement("UPDATE orders SET state='已支付' WHERE order_id=? AND state='待支付'");){
            ptmt.setString(1,orderId);
            ptmt.executeUpdate();
            LogService.logAction(username, "支付订单: " + orderId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    public static void cancelOrder(String username,String orderId) {
        try(PreparedStatement ptmt=conn.prepareStatement("UPDATE orders SET state='已取消' WHERE order_id=? ");){
            ptmt.setString(1,orderId);
            ptmt.executeUpdate();
            LogService.logAction(username, "取消订单: " + orderId);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}
